using Microsoft.Extensions.DependencyInjection;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.Services;
using JGSY.CMS.LowCode.Platform.Infrastructure.Configuration;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.ServiceCollectionExtensions
{
    /// <summary>
    /// 应用服务扩展
    /// </summary>
    public static class AppServiceCollectionExtensions
    {
        /// <summary>
        /// 注册应用服务
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddApplicationServices(this IServiceCollection services)
        {
            // 核心业务服务
            services.AddScoped<ICategoryAppService, CategoryAppService>();
            services.AddScoped<IArticleAppService, ArticleAppService>();
            services.AddScoped<IPageAppService, PageAppService>();
            services.AddScoped<IFormAppService, FormAppService>();
            services.AddScoped<IWorkflowAppService, WorkflowAppService>();
            
            // 用户和权限服务
            services.AddScoped<IUserAppService, UserAppService>();
            services.AddScoped<IRoleAppService, RoleAppService>();
            services.AddScoped<IPermissionAppService, PermissionAppService>();
            services.AddScoped<IUserRoleAppService, UserRoleAppService>();
            services.AddScoped<IUserDeviceAppService, UserDeviceAppService>();
            services.AddScoped<ITwoFactorAppService, TwoFactorAppService>();
            
            // 组织架构服务
            services.AddScoped<IDepartmentAppService, DepartmentAppService>();
            services.AddScoped<ITeamAppService, TeamAppService>();
            
            // 内容管理服务
            services.AddScoped<IContentAppService, ContentAppService>();
            services.AddScoped<ITemplateAppService, TemplateAppService>();
            services.AddScoped<IMediaFileAppService, MediaFileAppService>();
            services.AddScoped<IProductAppService, ProductAppService>();
            services.AddScoped<IPublishingAppService, PublishingAppService>();
            
            // 系统服务
            services.AddScoped<ISystemAppService, SystemAppService>();
            services.AddScoped<ISystemSettingAppService, SystemSettingAppService>();
            
            // 安全服务
            services.AddScoped<ISecurityAppService, SecurityAppService>();
            services.AddScoped<ISecurityEventAppService, SecurityEventAppService>();
            services.AddScoped<IRiskAssessmentAppService, RiskAssessmentAppService>();
            services.AddScoped<ISocialAuthAppService, SocialAuthAppService>();
            
            // 通信服务
            services.AddScoped<ISmsAppService, SmsAppService>();
            
            // 配置管理服务
            services.AddScoped<StorageConfigurationUnifier>();
            services.AddScoped<CentralizedStorageConfigurationManager>();
            services.AddScoped<ConfigurationMigrationService>();
            
            // 工具服务
            // services.AddScoped<IUserContextService, UserContextService>(); // 待实现
            
            // 多租户管理服务 (Phase 4 - 高级SaaS功能) - 临时注释，等待实现
            // services.AddScoped<ITenantResourceQuotaService, TenantResourceQuotaService>();
            // services.AddScoped<ITenantMonitoringService, TenantMonitoringService>();
            // services.AddScoped<ITenantDataManagementService, TenantDataManagementService>();
            
            return services;
        }
        
        /// <summary>
        /// 注册核心应用服务（最小化依赖）
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddCoreApplicationServices(this IServiceCollection services)
        {
            // 只注册核心必需的服务
            services.AddScoped<ICategoryAppService, CategoryAppService>();
            services.AddScoped<IArticleAppService, ArticleAppService>();
            services.AddScoped<IUserAppService, UserAppService>();
            services.AddScoped<IUserContextService, UserContextService>();
            
            // 注册身份验证服务
            services.AddScoped<IAuthenticationAppService, Application.Services.AuthenticationAppService>();
            
            return services;
        }
    }
}